<?php

namespace App\Http\Controllers\Foundation;

use App\Model\System\SystemConfig;
use App\Model\System\User;
use App\Model\Foundation\Employee;
use Illuminate\Http\Request;
use Laravel\Lumen\Routing\Controller as BaseController;
use App\Model\System\Token;
use App\Http\Controllers\MessagePacket;


class EmployeeController extends BaseController
{
    use MessagePacket;

    /**
     * 获取单个雇员信息
     * @param $id
     * @return packetMessage
     */
    function get($id)
    {
        $id = intval($id);
        $employe = Employee::find($id);
        if(empty($employe)){
            return $this->fail(false, '数据不存在');
        }
        return $this->success($employe);
    }


    /**
     * 批量查询学生列表
     * @param Request $request
     * @return packetMessage
     */
    function gets(Request $request)
    {
        try{
            $query = Employee::wherenotNull('id');
            $query->where(function ($query) use ($request){

                if($request->has('account')) {
                    $name= $request->get('account');
                    $query->orwhere('name', 'like', "%{$name}%");
                }
                if($request->has('number')){
                    $query->orwhere('number', 'like', "%{$request->get('number')}%");
                }
                if($request->has('email')){
                    $query->orwhere('email', 'like', "%{$request->get('email')}%");
                }
            });

            # 需要／不需要 分页
            if($request->has('page')) {
                $limit = 30;
                if($request->has('perPage')){
                    $limit = $request->get('perPage');
                }
                $obj = $query->paginate($limit)->toArray();
                foreach ($obj['data'] as &$item){
                    $account = User::where('userid',$item['userid'])->pluck('account')->first();
                    $item['account'] = $account;
                }
                $data['list'] = $obj['data'];
                unset($obj['data']);
                $data['pager'] = $obj;
            }else{
                $data = $query->get();
            }
            #
            if(empty($data)){
                return $this->fail($data, '数据不存在');
            }
            return $this->success($data);
        }catch (\Exception $e){
            return $this->fail('查询失败'.$e->getMessage());
        }

    }


    /**
     * 添加
     * @param Request $request
     * @return packetMessage
     */
    function  create(Request $request)
    {
        try{
            $fields = $request->all();
            # 排重
            $employee = Employee::where('name', $fields['name'])->first();
            if($employee){
                return $this->fail(false,'名字存在');
            }
            # 机构人员表电话号排重
            $employee = Employee::where('number', $fields['number'])->first();
            if($employee){
                return $this->fail(false,'手机号已存在');
            }
            # 创建
            $obj = new Employee($fields);
            $obj->created_by = Token::user()->userid;
            $obj->updated_by = Token::user()->userid;
            # 关联用户表
            $user = User::where('number',$fields['number'])->first();
            if(empty($user)) {
                $user = new User();
                $user->userid = User::generateID();
                $user->type = User::ADMIN;
                $user->email = $fields['email'];
                $user->number = $fields['number'];
                $user->islock = User::UNLOCK;
                $password = '123456';
                $config = SystemConfig::find(1);
                if($config){
                    $password = $config->defaultpassword;
                }
                $user->setSecurityCode($password);
                $user->updateCreator();
                $obj->userid = $user->userid;
                $user->save();
            }else{
                $obj->userid = $user->userid;
            }
            $obj->save();
            return $this->success($obj);
        }catch (\Exception $e) {
            return $this->fail(false, '保存失败: ' . $e->getMessage());
        }
    }


    /**
     * 更新某个学员的信息
     * @param Request $request
     * @return packetMessage
     */
    function  update(Request $request, $id)
    {
        try{
            $obj = Employee::find($id);
            #修改手机号，如果手机号不存在，则更新用户表。
            if(!User::updateMobile($request, $obj,  'number')){
                return $this->fail(false,'手机号已经存在，不能更改.');
            }
            $obj->update($request->all());
            return $this->success(true);
        }catch (\Exception $e){
            return $this->fail(false,'修改失败:'.$e->getMessage());
        }
    }


    /**
     * @param Request $request
     * @param $id
     * @return  packetMessage
     */
    function disable(Request $request, $id)
    {
        $obj = Employee::find($id);
        $obj->status = Employee::DISABLE;
        if($obj->save()){
            return $this->success(true);
        }
        return $this->fail(false, '禁用失败');

    }

    /**
     * @param Request $request
     * @param $id
     * @return  packetMessage
     */
    function enable(Request $request, $id)
    {
        $obj = Employee::find($id);
        $obj->status = Employee::ENABLE;
        if($obj->save()){
            return $this->success(true);
        }
        return $this->fail(false, '启用失败');

    }

    /**
     * 关联用户id
     * @param Request $request
     * @param $id
     * @return array
     */
    function associate(Request $request, $id)
    {
        if(!$request->has('userid')){
            return $this->fail(false, '参数不全');
        }

        $obj = Employee::find($id);
        $obj->userid = $request->userid;
        if($obj->save()){
            return $this->success(true);
        }
        return $this->fail(false, '关联失败');
    }


    /**
     * 删除某个学员
     * @param $id
     * @return packetMessage
     */
    function  delete($id)
    {
        $stat = Employee::destroy($id);
        if($stat){
            return $this->success(true);
        }
        return $this->fail(false, '删除失败');

    }

    /**
     * 根据userid获取雇员信息
     * @param $id：userid
     * @return array
     */
    function getEmployeeByUserid($id)
    {
        $userid = intval($id);
        $employee = Employee::where('userid',$userid)->first();
        if(empty($employee)){
            return $this->fail($employee, '数据不存在');
        }
        return $this->success($employee);
    }
}
